package com.lmpop.trade.ti_keyword;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.dao.IbatisDbmImpl;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.RandomID;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ti_keywordInfo
  implements TradeInterf
{
  QueryList queryList;

  public Ti_keywordInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_keyword ti_keyword = new Ti_keyword();
    ti_keyword.setKey_id(map.getString("key_id"));
    ti_keyword.setKey_word(map.getString("key_word"));
    ti_keyword.setLocation(map.getInt("location"));
    ti_keyword.setKey_type(map.getString("key_type"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_keyword);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_keyword ti_keyword)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_keyword", ti_keyword);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);

    map.setString("attach_root_id", pkid_string);
    map.setString("attach_sign", "delete");

    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_keyword", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_keyword ti_keyword = new Ti_keyword();
    ti_keyword.setKey_id(map.getString("key_id"));
    ti_keyword.setKey_word(map.getString("key_word"));
    ti_keyword.setLocation(map.getInt("location"));
    ti_keyword.setKey_type(map.getString("key_type"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_keyword);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_keyword ti_keyword)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_keyword", ti_keyword);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Hashtable params = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    params.put("start", Integer.valueOf(start));
    params.put("limit", Integer.valueOf(limit));

    return this.queryList.getList("getListByTi_keywordPage", params);
  }

  public int getCountByObj(Object obj)
  {
    Hashtable params = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTi_keyword", params);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_keywordByPk", pk);
  }

  public List getListByKeyWord(String keyWord) {
    return this.queryList.getList("getTi_keywordBykeyWord", keyWord);
  }

  public int getYMaxByType(String pk)
  {
    List list = this.queryList.getList("getY_maxkeyword", pk);
    Hashtable map = new Hashtable();

    int i = 1;

    if ((list != null) && (list.size() > 0)) {
      map = (Hashtable)list.get(0);
      if (map.get("y_max") != null) {
        int num = Integer.parseInt(map.get("y_max").toString());
        while (num > 10) {
          i *= 10;
          num /= 10;
          if (num <= 10) {
            i *= (num + 1);
          }
        }
      }
    }
    return i;
  }

  public boolean updateKeyWordNum(Map ti_keyword)
  {
    boolean result = true;
    boolean isInsert = true;
    DbmInterf dbmMgr = new IbatisDbmImpl();
    List list = this.queryList.getList("justSearchKeyWordForCheck", ti_keyword);
    if ((list != null) && (list.size() > 0)) {
      isInsert = false;
    }

    String result_code = "0";
    if (isInsert) {
      RandomID randomID = new RandomID();
      String key_id = randomID.GenTradeId();
      ti_keyword.put("key_id", key_id);
      ti_keyword.put("location", "1");
      dbmMgr.updateSql("insertkeywordNum", ti_keyword);
    } else {
      dbmMgr.updateSql("updateKeyWordNum", ti_keyword);
    }

    return result;
  }

  public List getkeywordList()
  {
    Hashtable params = new Hashtable();
    return this.queryList.getList("getListByTi_keywordPage", params);
  }
}